package com.voice.call.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.voice.call.common.Const;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.UserLevel;
import com.voice.call.mapper.UserLevelMapper;
import com.voice.call.service.ConsumeLogService;
import com.voice.call.service.SupportMccService;
import com.voice.call.service.UserAccountService;
import com.voice.call.service.UserLevelService;
import com.voice.call.utils.DateUtil;

@Service
public class UserLevelServiceImpl implements UserLevelService{
	
	private static final Logger log = LoggerFactory.getLogger(UserLevelServiceImpl.class);
	
	@Resource
	private UserLevelMapper userLevelMapper;
	
	@Resource
	private ConsumeLogService consumeLogService;
	
	@Resource
	private UserAccountService userAccountService;
	
	@Resource
	private SupportMccService supportMccService;
	

	@Override
	public UserLevel getUserLevel(Integer integral){
		return userLevelMapper.getUserLevelForIntegral(integral);
	}
	
	@Override
	public Result getUserLevelList(Integer user_id){
		
		int total_integral = consumeLogService.getTodayIntegral(user_id);
		UserLevel level = this.getUserLevel(total_integral);
		int currentLevel = -1;
		if (level!=null) {
			currentLevel = level.getLevel().intValue();
		}
		

		String nowString=DateUtil.getNowDateTimeString(DateUtil.DATE_FMT);
		Map<String, Object> paramsMap =new HashMap<String, Object>();
		paramsMap.put("userId", user_id);
		paramsMap.put("type", Const.TASK_LEVEL);
		paramsMap.put("startTime", nowString+" 00:00:00");
		paramsMap.put("endTime", nowString+" 23:59:59");
		
		List<UserLevel>  list = userLevelMapper.list(paramsMap);
		List<Map<String, Object>> rsList = new ArrayList<Map<String,Object>>();
		if (list!=null && !list.isEmpty()) {
			
			for (UserLevel item : list) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", item.getId());
				map.put("level", item.getLevel());
				map.put("integral", item.getIntegral());
				map.put("cumulation", item.getFrom());
				if (currentLevel >=item.getLevel().intValue()) {
					map.put("status", item.getIsReceive().equals(Const.Y)?"1":"0");
				}else {
					map.put("status", "2"); //等级未达到
				}
				
				rsList.add(map);
			}
			
		}
		return new Result(rsList);
	}
	
	@Transactional
	@Override
	public Result receiveLevelIntegral(Integer user_id,Integer id,String ip){
		
		if(!supportMccService.isSupportCountry(ip)){
			log.info("不支持注册国家，用户等级积分兑换");
			return new Result(ResultEnum.ILLEGAL_INTERVAL_ERROR);
		}
		
		UserLevel level = userLevelMapper.get(id);
		if (level == null) {
			return new Result(ResultEnum.USER_LEVEL_NOT_EXISTS);
		}
		int currentLevel = this.getCurrentLevel(user_id);
		if (level.getLevel().intValue() > currentLevel) {
			return new Result(ResultEnum.USER_LEVEL_NOT_ARRIVED);
		}
		if(consumeLogService.existsLevelLogsForUserId(user_id, level.getLevel())){ //已领取
			return new Result(ResultEnum.USER_LEVEL_RECEIVE_ERROR);
		}
		
		if(userAccountService.updateAccountBlance(user_id, level.getIntegral(), true)){
			log.info("添加等级积分成功");
			//2. 添加消费记录
			if(consumeLogService.addConsumeLog(user_id, Const.TASK_LEVEL, Const.TASK_LEVEL,level.getIntegral(),true, String.valueOf(level.getLevel()))){
				log.info("添加等级积分记录成功");
				Map<String, Object> rsMap = new HashMap<String, Object>();
				rsMap.put("integral", level.getIntegral());
				return new Result(rsMap);
			}else {
				log.error("添加等级积分记录失败");
			}
		}else {
			log.error("添加等级积分失败");
		}
		
		return new Result(ResultEnum.FAILED);
		
	}
	
	//获取当前用户等级
	private int getCurrentLevel(Integer user_id) {
		
		int total_integral = consumeLogService.getTodayIntegral(user_id);
		UserLevel level = this.getUserLevel(total_integral);
		int currentLevel = 1;
		if (level!=null) {
			currentLevel = level.getLevel().intValue();
		}
		return currentLevel;
	}
}
